package cn.itsource.org.service.impl;

import cn.itsource.common.service.BaseService;
import cn.itsource.common.service.impl.BaseServiceImpl;
import cn.itsource.common.util.PageList;
import cn.itsource.org.domain.Department;
import cn.itsource.org.dto.DepartmentDto;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;


    @Override
    @Transactional
    public void save(DepartmentDto departmentDto) {
        //id为空，就证明是新增
        if (departmentDto.getId() == null) {
            //创建一个部门实体对象
            Department department = new Department();
            //如果想要拷贝成功，那么属性要一致（name,sn）
            BeanUtils.copyProperties(departmentDto, department);
            //设置部门经理
            department.setManager_id(departmentDto.getManagerId());
            //保存部门   // 9
            departmentMapper.save(department);
            //父级部门  [2,5,8]
            List<Long> parentId = departmentDto.getParentId();
            if(parentId!=null && parentId.size() !=0){
                String dirPath = "/";
                for (Long pid : parentId) {
                    dirPath = dirPath + pid + "/";
                }
                //加上自身的id
                dirPath = dirPath + department.getId();
                department.setParent_id(parentId.get(parentId.size()-1));
                department.setDirPath(dirPath);
            }
            //修改部门
            departmentMapper.update(department);
        }else{
            //根据id把部门查询出来
            Department department = departmentMapper.findById(departmentDto.getId());
            //修改名字/编号/经理id/上级部门
            department.setName(departmentDto.getName());
            department.setSn(departmentDto.getSn());
            department.setManager_id(departmentDto.getManagerId());
            //清空上级
            department.setDirPath(null);
            department.setParent_id(null);
            List<Long> parentId = departmentDto.getParentId();
            if(parentId!=null && parentId.size() !=0){
                String dirPath = "/";
                for (Long pid : parentId) {
                    dirPath = dirPath + pid + "/";
                }
                //加上自身的id
                dirPath = dirPath + department.getId();
                department.setParent_id(parentId.get(parentId.size()-1));
                department.setDirPath(dirPath);
            }
            //修改部门
            departmentMapper.update(department);
        }
    }






    @Override
    public List<Department> queryTree() {
        //查询所有的部门列表
        List<Department> departments = departmentMapper.findAll();
        //把部门列表转为map集合(key:部门id， value就是部门对象)
        Map<Long, Department> map = departments.stream().collect(Collectors.toMap(Department::getId, dept -> dept));
        //该列表是我们自己组装的树形结构
        List<Department> tree = new ArrayList<Department>();
        //循环所有的部门列表
        for (Department department : departments) {
            //判断是否是顶级部门
            if(department.getParent_id() == null){
                tree.add(department);
            }else{//如果else，就证明该部门可能是2级，3级，4级....
                //获取父级部门
                Department parentDepartment = map.get(department.getParent_id());
                if(parentDepartment!=null){
                    //把当前部门添加到父级部门的子部门列表中
                    parentDepartment.getChildren().add(department);
                }
            }
        }

        return tree;


    }


}
